<!DOCTYPE html>
<html>
<head>
	<title>购物车</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link type="text/css" rel="stylesheet" href="../css/bootstrap.css">
	<link type="text/css" rel="stylesheet" href="../css/style.css">
	<script type="text/javascript" src="../js/jquery-3.1.1.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap.min.js"></script>
	<script type="text/javascript" src="../layer/layer.js"></script>
	<script type="text/javascript" src="../js/cart.js"></script>
	<script type="text/javascript" src="../js/auth.js"></script>
	<script type="text/javascript" src="../js/CookieShop.js"></script>
</head>
<body>
	<!--header-->
	<div class="header">
    <div class="container">
        <nav class="navbar navbar-default" role="navigation">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <h1 class="navbar-brand"><a href="./index"></a></h1>
            </div>
            <!--navbar-header-->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li><a href="./index.html">首页</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">商品分类<b class="caret"></b></a>
                        <ul class="dropdown-menu multi-column columns-2">
                            <li>
                                <div class="row">
                                    <div class="col-sm-12">
                                        <h4>商品分类</h4>
                                        <ul class="multi-column-dropdown" id="goodsTypeMenu">
                                            <li><a class="list" href="./goods_list.html">全部系列</a></li>
                                        </ul>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </li>
                    <li><a href="./goodsrecommend_list.html?recommendTypeId=2">热销</a></li>
                    <li><a href="./goodsrecommend_list.html?recommendTypeId=3">新品</a></li>


                </ul>
                <!--/.navbar-collapse-->
            </div>
            <!--//navbar-header-->
        </nav>
        <div class="header-info">
            <div class="header-right search-box">
                <a href="javascript:;"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></a>
                <div class="search">
                    <form class="navbar-form" action="./goods_search.html">
                        <input type="text" class="form-control" name="keyword">
                        <button type="submit" class="btn btn-default" aria-label="Left Align">搜索</button>
                    </form>
                </div>
            </div>

            <div class="header-right cart">
                <a href="./goods_cart.html">
                    <span class="glyphicon glyphicon-shopping-cart active" aria-hidden="true"><span class="card_num"></span></span>
                </a>
            </div>
            <div class="clearfix"> </div>
        </div>
        <div class="clearfix"> </div>
    </div>
</div>
	<!--//header-->
	<!--cart-items-->
	<div class="cart-items">
		<div class="container">
			<h2>我的购物车</h2>
			<div id="dgcontent">
			</div>
		</div>
	</div>
	<!--//cart-items-->
	<!--footer-->
	<div class="footer">
        <div class="container">
            <div class="text-center">

            </div>
        </div>
    </div>
	<!--//footer-->
</body>
<script type="text/javascript">
$(document).ready(function(){
  translateGoodses = translateListActiveGoodses();
  countMyCart();
  genGoodsTypesMenu();
  decorateMainMenu();
  loadData();
});

function loadData(){
	$.ajax({
		type:"post",
		url: "../../custcontroller/goodsCustController/cart_list",
		data: {
			goodsTypeId:0,
			page:1,
		},
		dataType: 'json',
		success: function(data, textStatus) {
			debugger;
			if ( data.rows !=null ){
			   for (let i=0;i<data.rows.length;i++){
				  appendGridRow(data.rows[i]);
				}
				appendSummary(data.total);
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function appendGridRow(row){
    debugger;
    var goods = translateGoods(row.goodsId);
    appendGridRow2(row,goods);
}

function appendGridRow2(row,goods){
  var rowStr = "<div class=\"cart-header col-md-6\">\n"+
				"<div class=\"cart-sec simpleCart_shelfItem\">\n"+
				"<div class=\"cart-item cyc\">\n"+
				"<a href=\"./goods_detail.html?id="+row.goodsId+"\">\n"+
				"<img src=\"data:image/png;base64,"+goods.cover+"\" class=\"img-responsive\">\n"+
				"</a>\n"+
				"</div>\n"+
				"<div class=\"cart-item-info\">\n"+
				"<h3><a href=\"./goods_detail.html?id="+row.goodsId+"\">"+goods.name+"</a></h3>\n"+
				"<h3><span>单价: ¥ "+row.price+"</span></h3>\n"+
				"<h3><span>数量: "+row.amount+"</span></h3>\n"+
				"<a class=\"btn btn-info\" href=\"javascript:buy("+row.goodsId+",1);\">增加</a>\n"+
				"<a class=\"btn btn-warning\" href=\"javascript:lessen("+row.goodsId+",1);\">减少</a>\n"+
				"<a class=\"btn btn-danger\" href=\"javascript:deletes("+row.goodsId+");\">删除</a>\n"+
				"</div>\n"+
				"<div class=\"clearfix\"></div>\n"+
				"</div>\n"+
				"</div>\n";
    $("#dgcontent").append(rowStr);
}

function appendSummary(total){
    var rowStr = "<div class=\"cart-header col-md-12\">\n"+
                "<hr>\n"+
				"<h3>订单总金额: ¥ "+total+"</h3>\n"+
				"<a class=\"btn btn-success btn-lg\" style=\"margin-left:74%\" href=\"javascript:custOrderSubmit()\">提交订单</a>\n"+
				"</div>\n";
	$("#dgcontent").append(rowStr);
}

function order_submit(){
  alert("haha!");
}

function translateGoods(value){
	if (translateGoodses != null && translateGoodses != undefined && translateGoodses.length > 0){
		for(var i=0;i<translateGoodses.length;i++){
			if (translateGoodses[i].id == value) return translateGoodses[i];
		}
	}
	return null;
}

function translateListActiveGoodses(value){
	var retVal = [];
	$.ajax({
		type:"post",
		url: "../custcontroller/goodsCustController/listActiveGoodses",
		data: {
			id:value
		},
		dataType: 'json',
		async:false,
		success: function(data, textStatus) {
			debugger;
			if ( data.rows !=null ){
				retVal = data.rows;
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
	return retVal;
}


function custOrderSubmit(){
    $.ajax({
		type:"post",
		url: "../custcontroller/goodsCustController/cust_order_submit",
		dataType: 'json',
		success: function(data, textStatus) {
			debugger;
			if(data.success == true)
			{
			   window.location.href = "./order_submit.html";
			}
			else if(data.success == false)
			{
              layer.msg(data.error, {time:3000}, function(){
              });
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}
</script>
</html>
